package com.itheima.health.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.resource.entity.QueryPageBean;
import com.itheima.health.resource.pub.RedisConstant;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetmealService;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {


    @Autowired
    private CheckGroupMapper checkGroupMapper;



   @Autowired
   RedisTemplate<Object,Object> redisTemplate;
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
         //     添加套餐   更新中间表数据    套餐表和检查组关系表
        saveOrUpdate(setmeal);  // 如果存在id ---update  如果不存在id  save
        int setmealId = setmeal.getId(); //  获取检查组的主键
        if(checkgroupIds!=null&&checkgroupIds.length!=0){
            for (Integer groupId : checkgroupIds) {
                //  更新中间表   检查组和检查项中间表
                baseMapper.addCheckGroupAndSetmealIds(setmealId,groupId);
            }
        }

        //   图片 存储redis
        redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }

    @Override
    public Map findPageByContion(QueryPageBean queryPageBean) {
        // LambdaQueryWrapper 条件构造器
        LambdaQueryWrapper<Setmeal> lambda = new QueryWrapper<Setmeal>().lambda();
        String queryString = queryPageBean.getQueryString();//  条件
        queryString=queryString==null?"":queryString;
        //  对象名::方法名
        lambda.like(Setmeal::getName,queryString).or().like(Setmeal::getHelpCode,queryString);
        //  select  * from xxx where is_delete=0 and (name like  xx  or  helpCode like "")
        //  分页请求参数条件
        Page<Setmeal> pageResult = page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), lambda);
        List<Setmeal> setmealList = pageResult.getRecords();
        long total = pageResult.getTotal();
        Map map = new HashMap(); //  定义一个Map对象 封装分页查询结果集数据
        map.put("list",setmealList);  //  分页数据
        map.put("total",total);  // 总记录数
        return map;
    }

    @Override
    public Setmeal findSetMealDetailBySetmealId(Integer id) {
        Object checketGroupById = redisTemplate.opsForValue().get("checkGroupBy" + id);

        if (checketGroupById!=null) {
            String str = String.valueOf(checketGroupById);
            Setmeal setmeal1 = JSON.parseObject(str, Setmeal.class);
//            System.out.println(setmeal1);
            System.out.println("reids=========================================");
            return setmeal1;
        }else {
            //   1 查询 套餐基本信息多表查询
            Setmeal setmeal = baseMapper.selectById(id);//   查询基本 套餐信息}
            //   2.根据套餐id  查询对应得检查组信息
            List<CheckGroup> checkGroupList = baseMapper.findCheckGroupsBySetmealId(id);

            //   3. 根据检查组id  查询对应检查项信息
            for (CheckGroup checkGroup : checkGroupList) {
                List<CheckItem> checkItemList = checkGroupMapper.findCheckItemsByGroupId(checkGroup.getId());
                checkGroup.setCheckItems(checkItemList);
            }
            setmeal.setCheckGroups(checkGroupList);
            redisTemplate.opsForValue().set("checkGroupBy" + id, setmeal);
            System.out.println("数据库=========================================");
            return setmeal;
        }
    }

    @Override
    public Setmeal findSetmealById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<Map> getSetmealZhanBi() {

        return baseMapper.getSetmealZhanBi();
    }

    @Override
    public List<Setmeal> findAllSetmeal() {
        Object setmeals = redisTemplate.opsForValue().get("setmeals");
        if (setmeals == null) {
            redisTemplate.opsForValue().set("setmeals",list());
            return list();
        } else {
            System.out.println("--------");
            return (List<Setmeal>) setmeals;
        }
    }

}
